package controllers

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"qytest/models"
)

type MainController struct {
	beego.Controller
}

var o = orm.NewOrm()

func (c *MainController)Get() {

	c.TplName = "login.html"
}

func (c *MainController)Post() {
	//c.Ctx.WriteString("登陆成功")

	//1.拿到数据
	userName := c.GetString("userName")
	pwd := c.GetString("pwd")

	//2.判断数据是否合法
	if userName == "" || pwd == "" {
		beego.Info("数据不能为空")
		c.Redirect("/login",302)
		return
	}

	//3.查询账号密码是否正确
	user := models.User{}
	user.Name = userName


	err := o.Read(&user,"Name")
	if err != nil {
		beego.Info("用户名错误")
		//c.Ctx.WriteString("用户名错误")
		c.Redirect("/login",302)
		return
	}
	if user.Pwd == pwd {
		c.SetSession("userName",userName)
		//4.跳转
		c.Redirect("/Manage/user/index",302)
	}else {
		beego.Info("密码错误")
		//c.Ctx.WriteString("密码错误")
		c.Redirect("/login",302)
		return
	}

}

func (c*MainController)ShowLogout() {
	c.DelSession("userName")
	c.Redirect("/login",302)
}

